﻿using UnityEditor;
using UnityEngine;

class TextureImporterWebGL : ITextureImpoter
{
    public void Import(TextureImporter importer, TextureImporterType type)
    {
        TextureImporterPlatformSettings webGl = importer.GetPlatformTextureSettings("WebGL");
        if (webGl != null && webGl.overridden) return;

        webGl ??= new TextureImporterPlatformSettings();
        webGl.overridden = true;
        webGl.name = "WebGL";
        bool isClamp1024 = true;
        bool isCrunch = true;

        switch (type)
        {
            case TextureImporterType.None:
                break;
            case TextureImporterType.Small:
                importer.textureType = UnityEditor.TextureImporterType.Sprite;
                importer.mipmapEnabled = false;
                importer.alphaIsTransparency = true;
                importer.wrapMode = TextureWrapMode.Clamp;
                webGl.format = TextureImporterFormat.ASTC_4x4;
                break;
            case TextureImporterType.Big:
                webGl.format = TextureImporterFormat.ASTC_6x6;
                importer.wrapMode = TextureWrapMode.Clamp;
                break;
            case TextureImporterType.Effect:
                webGl.format = TextureImporterFormat.ASTC_6x6;
                break;
            case TextureImporterType.Animation:
                importer.wrapMode = TextureWrapMode.Clamp;
                webGl.format = TextureImporterFormat.ASTC_6x6;
                isClamp1024 = false;
                isCrunch = false;
                break;
            default:
                break;
        }

        if (isClamp1024 && webGl.maxTextureSize > 1024)
            webGl.maxTextureSize = 1024;
        if (isClamp1024 && importer.maxTextureSize > 1024)
            importer.maxTextureSize = 1024;
        importer.textureCompression = TextureImporterCompression.Compressed;
        importer.crunchedCompression = isCrunch;
        importer.compressionQuality = 60;
        importer.SetPlatformTextureSettings(webGl);
        importer.SaveAndReimport();
    }
}


